package com.jxs.android.ges;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Handler;
import android.os.Message;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.util.HandlerHolder;

public class Visit_Records_SaveHolder {

	private Handler handler;
	private String record_id = "";// 工作日志ID

	// 2.4.5 saveWorkLog 【保存工作日志接口】
	public Visit_Records_SaveHolder(Handler handler, String record_id) {
		this.handler = handler;
		this.record_id = record_id;
	}

	public void saveVisitsRecordX(final String ticket, final String create_date, final String cust_id, final String cust_name, final String basic, final boolean way_phone,
			final boolean way_site) {

		new Thread(new Runnable() {
			@Override
			public void run() {
				// 2.5.4 saveVisitsRecordX
				String wsdl_url = GesApplication.WSDL_URL;// wsdl
				String namespace = GesApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "saveVisitsRecordX";//
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", ticket);
				request.addProperty("record_id", record_id);// 走访记录ID
				request.addProperty("cust_id", cust_id);// 客户ID
				request.addProperty("cust_name", cust_name);// 客户名称
				request.addProperty("create_date", create_date);// 日期
				request.addProperty("basic", basic);// 拜访基本情况
				request.addProperty("way_phone", (way_phone ? "1" : ""));// 电话拜访（1选中，其他未选中）
				request.addProperty("way_site", (way_site ? "1" : ""));// 现场拜访（1选中，其他未选中）

				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						SoapObject response = (SoapObject) envelope.getResponse();
						paser_new_Interface(response);
					}

					@Override
					public void error(String message, Exception e) {
						Message msg = new Message();
						msg.what = HandlerHolder.What.ERROR_MSG;
						msg.obj = message;
						handler.sendMessage(msg);
					}

				});
			}
		}).start();
	}

	/**
	 * @Des this method will be used to when update interface protocol
	 * @param soapObject
	 * @param params
	 */
	private void paser_new_Interface(SoapObject soapObject, Object... params) {
		if (null != soapObject) {
			String stauts = soapObject.getProperty("status").toString();
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getProperty("message").toString();
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = HandlerHolder.What.ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = HandlerHolder.What.TO_LOGIN;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("0".equals(stauts)) {
				Message msg = new Message();
				msg.what = HandlerHolder.What.VISIT_RECORDS_SAVE_RESLT_OK;
				msg.obj = message;
				handler.sendMessage(msg);
			}
		}
	}

}
